#!/bin/bash

#  Script to install the RAIDCore drivers from the SDK for supported platforms
#  (RedHat and Ubuntu). 

#
# Copyright © 2006-2008 Ciprico Inc. All rights reserved.
# Copyright © 2008-2013 Dot Hill Systems Corp. All rights reserved.
# Copyright © 2020-2023 Advanced Micro Devices, Inc. All rights reserved.
#
# Use of this software is subject to the terms and conditions of the written
# software license agreement between you and AMD (the "License"),  
# including, without limitation, the following (as further elaborated in the
# License):  (i) THIS SOFTWARE IS PROVIDED "AS IS", AND AMD DISCLAIMS
# ANY AND ALL WARRANTIES OF ANY KIND, WHETHER EXPRESS, IMPLIED, STATUTORY,
# BY CONDUCT, OR OTHERWISE; (ii) this software may be used only in connection
# with the integrated circuit product and storage software with which it was
# designed to be used; (iii) this source code is the confidential information
# of AMD and may not be disclosed to any third party; and (iv) you may not
# make any modification or take any action that would cause this software,
# or any other Advanced Micro Devices software, to fall under any GPL license
# or any other open source license.
#

source src/common_shell

USE_SWL="ahci"
FORCE=$1
install_modules(){
    (cd src; sh ./$1 $2 $3 $4 $5)
    if [ $? -ne 0 ]; then
        echo "ERROR: Failed to install the RAIDCORE Drivers."
        exit 1
    fi
}

build_modules(){
    KERNELMAKEFILE=/lib/modules/$1/build/Makefile
    buildfailed=0

    if [ -f $KERNELMAKEFILE ]; then
        (cd src; make clean; make KVERS=$1 all);
        if [ $? -ne 0 ]; then
            buildfailed=1
        fi
    else
        buildfailed=1
    fi

    if [ $buildfailed -eq 1 ]; then	
        echo "ERROR: Failed to build the RAIDCORE Drivers."
        echo "Please make sure kernel source rpm is installed for $1."
    fi
}

# Builds and installs the driver for RedHat variants
build_and_install_rh(){
    set_kernel_flavors_rh
    for flavor in $flavors ; do
        if [ $flavor = "uni" ] ; then
            version=$kernel
        else
            version=$kernel$flavor
        fi
                
        build_modules $version
        if [ -f "src/rcraid.ko" ] ; then
            install_modules install_rh "USE_SWL=${USE_SWL}" $version
        else
            echo "#"
            echo "# Driver module not built -- install aborted!"
            echo "#"
        fi
    done
    KERNELS=$(ls /lib/modules)
    for RELEASE in $KERNELS
    do
        if [ -d "/lib/modules/$RELEASE/kernel/drivers/scsi" ] ; then
                if [ ! -f "/lib/modules/$RELEASE/extra/rcraid/rcraid.ko" ] && \
			[ ! -f "/lib/modules/$RELEASE/kernel/drivers/scsi/rcraid.ko" ]; then
                        build_modules $RELEASE
                        install_modules install_rh "USE_SWL=${USE_SWL}" $RELEASE
                fi
        fi
    done
}

build_and_install_ubuntu(){
	KERNELS=$(ls /lib/modules)
	RCRAID="rcraid-9.3.2"

	ln -s rcblob.i386 src/rcblob.i686

	if [ ! -f "/usr/src/$RCRAID/mk_certs" ] ; then
		mkdir -p /usr/src/$RCRAID
		cp -Rap ../driver_sdk/* /usr/src/$RCRAID
		#/usr/bin/echo  "MAKE=\"make -C /usr/src/$RCRAID KERNELDIR=/lib/modules/\${kernelver}/build\"" > /ii#usr/src/$RCRAID/dkms.conf
		#/usr/bin/echo  "CLEAN=\"make -C /usr/src/$RCRAID clean\"" >> /usr/src/$RCRAID/dkms.conf
		echo  "BUILT_MODULE_NAME=rcraid" > /usr/src/$RCRAID/dkms.conf
		echo  "BUILT_MODULE_LOCATION=/usr/src/$RCRAID" >> /usr/src/$RCRAID/dkms.conf
		echo  "PACKAGE_NAME=rcraid" >> /usr/src/$RCRAID/dkms.conf
		echo  "PACKAGE_VERSION=9.3.2" >> /usr/src/$RCRAID/dkms.conf
		#/usr/bin/echo  "REMAKE_INITRD=yes" >> /usr/src/$RCRAID/dkms.conf
		echo  "DEST_MODULE_NAME=rcraid" >> /usr/src/$RCRAID/dkms.conf
		echo  "DEST_MODULE_LOCATION=/kernel/drivers/scsi" >> /usr/src/$RCRAID/dkms.conf
		echo  "AUTOINSTALL=yes" >> /usr/src/$RCRAID/dkms.conf
		echo  "PRE_BUILD=/install" >> /usr/src/$RCRAID/dkms.conf

		dkms status | grep "rcraid, 9.3.2"
		if [ $? -ne 0 ]; then
			dkms add -m rcraid -v 9.3.2
		fi
	fi

	chmod -R 777 /usr/src/$RCRAID/*

	dkms status | grep "rcraid, 9.3.2"
	if [ $? -ne 0 ]; then
		dkms add -m rcraid -v 9.3.2
	fi

        DEBIAN="0"
	grep "SMP Debian" /proc/version
        if [ $? -eq 0 ]; then
        	DEBIAN="debian"
        fi

	dkms status 
	for RELEASE in $KERNELS
	do
		if [ ! -f "/lib/modules/$RELEASE/kernel/drivers/scsi/rcraid.ko" ] || \
			[ "$DEBIAN" = "debian" ] || [ "$FORCE" = "force" ]; then
			echo "Make kernel for $RELEASE"
			make -C src clean
			make -C src KVERS=$RELEASE

			if [ -f "src/rcraid.ko" ] ; then
				## update modules.dep
				mv /lib/modules/$RELEASE/kernel/drivers/scsi/rcraid.ko /lib/modules/$RELEASE/kernel/drivers/scsi/rcraid.ko.bak
				cp -ap src/rcraid.ko /lib/modules/$RELEASE/kernel/drivers/scsi/rcraid.ko
				depmod -a $RELEASE

				## make new initrd.img
				cp -ap /boot/initrd.img-$RELEASE /boot/initrd.img-$RELEASE.bak

				#rm -rf /etc/modprobe.d/blacklist.local.conf
				mkinitramfs -o /boot/initrd.img-$RELEASE $RELEASE
				if [ "$FORCE" = "force" ]; then
					mkdir -p /usr/src/$RCRAID
					cp -Rap ../driver_sdk/* /usr/src/$RCRAID
					#/usr/bin/echo  "MAKE=\"make -C /usr/src/$RCRAID KERNELDIR=/lib/modules/\${kernelver}/build\"" > /ii#usr/src/$RCRAID/dkms.conf
					#/usr/bin/echo  "CLEAN=\"make -C /usr/src/$RCRAID clean\"" >> /usr/src/$RCRAID/dkms.conf
					echo  "BUILT_MODULE_NAME=rcraid" > /usr/src/$RCRAID/dkms.conf
					echo  "BUILT_MODULE_LOCATION=/usr/src/$RCRAID" >> /usr/src/$RCRAID/dkms.conf
					echo  "PACKAGE_NAME=rcraid" >> /usr/src/$RCRAID/dkms.conf
					echo  "PACKAGE_VERSION=9.3.2" >> /usr/src/$RCRAID/dkms.conf
					#/usr/bin/echo  "REMAKE_INITRD=yes" >> /usr/src/$RCRAID/dkms.conf
					echo  "DEST_MODULE_NAME=rcraid" >> /usr/src/$RCRAID/dkms.conf
					echo  "DEST_MODULE_LOCATION=/kernel/drivers/scsi" >> /usr/src/$RCRAID/dkms.conf
					echo  "AUTOINSTALL=yes" >> /usr/src/$RCRAID/dkms.conf
					echo  "PRE_BUILD=/install" >> /usr/src/$RCRAID/dkms.conf

					dkms status | grep "rcraid, 9.3.2"
					if [ $? -ne 0 ]; then
						dkms add -m rcraid -v 9.3.2
					fi
				fi
			else
				echo "#"
				echo "# Driver module not built -- install aborted!"
				echo "#"
			fi
        else 
			echo "RCRAID for $RELEASE already exists"
		fi
	done

}

########
# MAIN #
########

if [ $UID != 0 ] ; then
    echo "This script must be run as root"
    exit 1
fi

if [ -e /etc/redhat-release ]; then
    # redhat based
    while [ $# -gt 0 ] ; do
        case $1 in
            --help) 
                usage_rh $1
                ;;
            USE_SWL=*)
                USE_SWL=${1:8}
                ;;
            uni | smp | bigmem | hugemem | largesmp | PAE)
                case " $flavors " in
                    *" $1 "*) ;;
                    *) flavors="$flavors $1";;
                esac
                ;;
            *)
                if [ -z $kernel ] ; then
                    kernel=$1
                else
                    usage_rh $1
                fi
                ;;
        esac
        shift
    done
    build_and_install_rh
else  
    # generic
    UBUNTU_RELEASE=$(grep 'Ubuntu' /etc/lsb-release)
    DEBIAN_RELEASE=$(cat /etc/debian_version)
    if [ -z UBUNTU_RELEASE ]; then
        if [ -z DEBIAN_RELEASE ]; then
            (cd src; make clean; make all; make install);
        else
            build_and_install_ubuntu
        fi
    else    
        build_and_install_ubuntu
    fi
    exit $?
fi
